#!/bin/bash

set -e

dataMnt=/mnt/server.lan/data
dataStorage=$dataMnt/dataStorage

flag=false
if [ ! -e "$dataStorage" ];then
  echo "[E] $dataStorage : dir not exists,please make it"
  flag=true
fi

if [ -z "$TZ" ];then
  # "Asia/Shanghai"
  echo '[ERR] env TZ  not set, and install "tzdata" '
  flag=true
fi

if $flag;then
  exit 1
fi

###################
## settings
name="server.lan"
image="inthbl/aria2-server"

docker pull "${image}"

mkDir() {
  if [ -z "$1" ]; then
    return
  fi

  if [ ! -e "${1}" ]; then
    mkdir -p "${1}"
    chown 1000:1000 "${1}"
  fi
}
dataDir="$dataStorage/$name"
mkDir "${dataDir}"


docker run -d --name "${name}" \
    --restart=always \
    -e TZ=$TZ \
    -v "$dataDir":/data \
    -v /usr/share/zoneinfo/$TZ:/etc/localtime:ro \
    -v /etc/timezone:/etc/timezone:ro \
    -p 127.0.0.1:6800:6800 \
    "${image}"

exit $?
